/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eschool.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.eschool.model.ForumPost;
import com.eschool.util.HibernateUtil;
import com.eschool.util.JdbcUtil;

/**
 *
 * @author Ammar
 */

public class ForumDao{
	
	
	public static boolean addPost(ForumPost post) {
    	
    	boolean isInserted = false;
    	
    	Connection connection = JdbcUtil.openConnection();
		
		try{
			CallableStatement storedProc = connection.prepareCall("{ call InsertForumPost(?,?,?,?,?) }");
			
			storedProc.setString(1, post.getMessage());
			storedProc.setString(2, post.getPostTime().toString());
			if(post.getParentPost() != null)
				storedProc.setInt(3, post.getParentPost().getPostId());
			else
				storedProc.setString(3, null);
			storedProc.setInt(4, post.getUser().getUserId());
			storedProc.setString(5, post.getSubject());

			storedProc.execute();
		
			isInserted = true;
			connection.close();
		}
		
		catch(Exception e){
			isInserted = false;
			connection.close();
			e.printStackTrace();
		}
		finally{
			return isInserted;
		}
	}

	
	public static ForumPost getPostByProperty(String property, Object value) {
		
		Session session = HibernateUtil.currentSession();
	      
		Criteria crit = session.createCriteria(ForumPost.class);
		crit.add(Restrictions.eq(property, value));
		
		ForumPost post = (ForumPost) crit.uniqueResult();

        HibernateUtil.closeSession(session);

        return post;
	}
	
	public static List getPosts(Map<String , Object> conditions) {
		
		Session session = HibernateUtil.currentSession();
		Criteria crit = session.createCriteria(ForumPost.class);
		
		if (conditions != null)
			
			for (Iterator iterator = conditions.entrySet().iterator(); iterator.hasNext();) {
			
				Map.Entry condition = (Map.Entry) iterator.next();
				
				crit.add(Restrictions.eq(((String) condition.getKey()), condition.getValue()));
			}
		
		List posts = (List<ForumPost>) crit.list();
		
        HibernateUtil.closeSession(session);
        
        if(posts == null)
        	return Collections.EMPTY_LIST;

        return posts;
	}
	
	public static List getParentPosts(int branchId) {
		Session session = HibernateUtil.currentSession();
		Criteria crit = session.createCriteria(ForumPost.class);
		crit.createAlias("user", "u").createAlias("u.branch", "b");
		
		crit.add(Restrictions.isNull("parentPost"));
		crit.add(Restrictions.eq("b.branchId",branchId));
		crit.addOrder(Order.desc("postTime"));
		
		List posts = (List<ForumPost>) crit.list();
		
        HibernateUtil.closeSession(session);
        
        if(posts == null)
        	return Collections.EMPTY_LIST;

        return posts;
	}
	
	
	/*
	public static List<SaasAdmin> getAllSaasAdmin() {
		Session session = HibernateUtil.currentSession();
	      
		Criteria crit = session.createCriteria(SaasAdmin.class);
		
		List admins = crit.list();
		
        HibernateUtil.closeSession(session);
        return admins;
	}*/
}
